package com.system.service;

import com.system.controller.TopicsController;
import com.system.dao.impl.MainDataMapper;
import com.system.menu.UI;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TopicsService {
    private final Scanner sc = new Scanner(System.in);
    private final TopicsController tc;
    private final MainDataMapper mdm;
    private final UI ui = new UI();

    public TopicsService() {
        this.tc = new TopicsController();
        this.mdm = new MainDataMapper();
    }

    public void menu(String email) {

        while (true) {
            if (!(this.mdm.selectHistoryAmount(email) == 0)) {
                ui.checkHistoryM(email);
                this.tc.getTdm().selectTopicsForUser(email); // 显示帖子
                System.out.println("请输入你的选择：");
                try {
                int optHistory = sc.nextInt();
                    switch (optHistory) {
                        case 1:
                            isShow(email);
                            break;
                        case 2:
                            return;
                        default:
                            System.out.println("输入有误！");
                            break;
                    }
                }catch(InputMismatchException e) {
                    System.out.println("输入错误，请输入数字！");
                    sc.next(); // 清除错误输入
                }
            } else {
                System.out.println("您的历史记录为空！");
                break;
            }
        }
    }

    // 是否要隐藏
    public void isShow(String email) {
        boolean flag = true;
        int cntInput = 0;
        while (flag) {
            System.out.println("如果需要隐藏帖子，请输入编号:");
            try {
                int inputId = sc.nextInt();
                if (inputId <= this.mdm.selectHistoryAmount(email)) {
                    if (this.tc.getTdm().ifShowForUser(inputId, email)) {
                        this.mdm.updateAllTopicDE(); // 同步 - 1
                        break;
                    } else {
                        System.out.println("该帖子已经被隐藏了");
                        flag = false;
                    }
                } else {
                    if (cntInput == 3) {
                        System.out.println("编号错误输入过多！");
                        break;
                    }
                    System.out.println("编号不正确！");
                    cntInput++;
                }
            }catch(InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }

}
